package cn.echoedu.service;
import static cn.echoedu.constants.Code.CLASS_LIST;
import static cn.echoedu.constants.Code.SCHOOL_CALENDAR;
import static cn.echoedu.constants.Code.CLASS_NOTIFY_FIRST;
import static cn.echoedu.constants.Code.CLASS_NOTIFY_LIST;
import static cn.echoedu.constants.Code.CLASS_NOTIFY_LIST_DETAIL;
import static cn.echoedu.constants.Code.CONTACTS_CLASS;
import static cn.echoedu.constants.Code.CONTACTS_LIST;
import static cn.echoedu.constants.Code.CONTACTS_MAKE_FRIEND;
import static cn.echoedu.constants.Code.DELETE_CLASS_NOTIFY;
import static cn.echoedu.constants.Code.DELETE_SCHOOL_NOTIFY;
import static cn.echoedu.constants.Code.FEED_BACK;
import static cn.echoedu.constants.Code.HOT_CONTACTS_LIST;
import static cn.echoedu.constants.Code.INDEX_PIC_QUERY;
import static cn.echoedu.constants.Code.LOGIN;
import static cn.echoedu.constants.Code.MONITOR_ADD_NOTIFY;
import static cn.echoedu.constants.Code.PROFESSIONAL_LIST;
import static cn.echoedu.constants.Code.QUERY_DEPARTMENT_LIST;
import static cn.echoedu.constants.Code.QUERY_SCHOOL_INFO;
import static cn.echoedu.constants.Code.QUERY_SCHOOL_LIST;
import static cn.echoedu.constants.Code.REGISTER;
import static cn.echoedu.constants.Code.SCHOOL_NOTIFY_FIRST;
import static cn.echoedu.constants.Code.SCHOOL_NOTIFY_LIST;
import static cn.echoedu.constants.Code.SCHOOL_NOTIFY_LIST_DETAIL;
import static cn.echoedu.constants.Code.STUDENT_LIST;
import static cn.echoedu.constants.Code.TEACHER_ADD_NOTIFY;
import static cn.echoedu.constants.Code.UPDATE_USER_BAIDU;
import static cn.echoedu.constants.Code.UPDATE_USER_BIRTHDAY;
import static cn.echoedu.constants.Code.UPDATE_USER_CLASSNAME;
import static cn.echoedu.constants.Code.UPDATE_USER_DEGRESS;
import static cn.echoedu.constants.Code.UPDATE_USER_ENTRANCETIME;
import static cn.echoedu.constants.Code.UPDATE_USER_HEAD_IMAGE;
import static cn.echoedu.constants.Code.UPDATE_USER_JOB;
import static cn.echoedu.constants.Code.UPDATE_USER_NAME;
import static cn.echoedu.constants.Code.UPDATE_USER_PASSWORD;
import static cn.echoedu.constants.Code.UPDATE_USER_POLITICALSTATUS;
import static cn.echoedu.constants.Code.UPDATE_USER_QQ;
import static cn.echoedu.constants.Code.UPDATE_USER_REALNAME;
import static cn.echoedu.constants.Code.UPDATE_USER_SEX;
import static cn.echoedu.constants.Code.UPDATE_USER_SINA;
import static cn.echoedu.constants.Code.UPDATE_USER_WECHAT;
import static cn.echoedu.constants.JsonCommand.EXCEPTION_ERROR;
import static cn.echoedu.constants.Code.SELECT_CONTACTS_LIST;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.echoedu.util.HttpUtil;
import cn.echoedu.util.L;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class BaseServletService extends BaseService{
	private final String T="BaseServletService----->";
	public void doHead(HttpServletRequest request,HttpServletResponse response){

	}
	public void doGet(HttpServletRequest request,HttpServletResponse response)
			throws IOException, SQLException{
		service(request,response);
	}
	public void doPost(HttpServletRequest request,HttpServletResponse response)
			throws IOException, SQLException{
		service(request,response);
	}
	public void service(HttpServletRequest request,HttpServletResponse response)
			throws IOException, SQLException{
		L.p(T+"service");
		String jsonstr=HttpUtil.readJSONString(request);
		L.p(T+jsonstr);
		L.p("$$$$$$$$$"+jsonstr+"$$$$$$$$$$$$");
		JsonElement je=new JsonParser() .parse(jsonstr);
		int code=getCode(je,response);
		switch (code){
		case CONTACTS_LIST:
			getReceiveService().doContactList(request,response,je);
			break;
		case CONTACTS_MAKE_FRIEND:
			getReceiveService().doContactsMakeFriend(request,response,je);
			break;
		case CONTACTS_CLASS:
			getReceiveService().doContactsClass(request,response,je);
			break;
		case HOT_CONTACTS_LIST:
			getReceiveService().doHotContactsList(request,response,je);
			break;
		case INDEX_PIC_QUERY:
			getReceiveService().doIndexPicQuery(request,response,je);
			break;
		case LOGIN:
			getReceiveService().doLogin(request,response,je);
			break;
		case REGISTER:
			getReceiveService().doRegister(request,response,je);
			break;
		case FEED_BACK:
			getReceiveService().doFeedBack(request,response,je);
			break;
		case UPDATE_USER_HEAD_IMAGE:
			getReceiveService().doUpdateHeadImage(request,response,je);
			break;
		case UPDATE_USER_NAME:
			getReceiveService().doUpdateUserName(request,response,je);
			break;
		case UPDATE_USER_REALNAME:
			getReceiveService().doUpdateUserRealName(request,response,je);
			break;
		case UPDATE_USER_SEX:
			getReceiveService().doUpdateUserSex(request,response,je);
			break;
		case UPDATE_USER_BIRTHDAY:
			getReceiveService().doUpdateUserBirthday(request,response,je);
			break;
		case UPDATE_USER_ENTRANCETIME:
			getReceiveService().doUpdateUserEntranceTime(request,response,je);
			break;
		case UPDATE_USER_PASSWORD:
			getReceiveService().doUpdateUserPassword(request,response,je);
			break;
		case UPDATE_USER_QQ:
			getReceiveService().doUpdateUserQQ(request,response,je);
			break;
		case UPDATE_USER_BAIDU:
			getReceiveService().doUpdateUserBaiDu(request,response,je);
			break;
		case UPDATE_USER_SINA:
			getReceiveService().doUpdateUserSina(request,response,je);
			break;
		case UPDATE_USER_WECHAT:
			getReceiveService().doUpdateUserWechat(request,response,je);
			break;
		case UPDATE_USER_CLASSNAME:
			getReceiveService().doUpdateUserClassName(request,response,je);
			break;
		case UPDATE_USER_JOB:
			getReceiveService().doUpdateUserJob(request,response,je);
			break;
		case UPDATE_USER_DEGRESS:
			getReceiveService().doUpdateUserDegress(request,response,je);
			break;
		case UPDATE_USER_POLITICALSTATUS:
			getReceiveService().doUpdateUserPoliticalStatus(request,response,je);
			break;
		case TEACHER_ADD_NOTIFY:
			getReceiveService().doTeacherAddNotify(request,response,je);
			break;
		case SCHOOL_NOTIFY_LIST:
			getReceiveService().doSchoolNotifyList(request,response,je);
			break;
		case SCHOOL_NOTIFY_LIST_DETAIL:
			getReceiveService().doSchoolNotifyListDetail(request,response,je);
			break;
		case PROFESSIONAL_LIST:
			getReceiveService().doProfessionalListQuery(request,response,je);
			break;
		case CLASS_LIST:
			getReceiveService().doClassListQuery(request,response,je);
			break;
		case STUDENT_LIST:
			getReceiveService().doStudentListQuery(request,response,je);
			break;
		case DELETE_SCHOOL_NOTIFY:
			getReceiveService().doDeleteSchoolNotify(request,response,je);
			break;
		case MONITOR_ADD_NOTIFY:
			 
			getReceiveService().doMonitorAddClassNotify(request,response,je);
			break;
		case CLASS_NOTIFY_LIST:
			getReceiveService().doClassNotifyList(request,response,je);
			break;
		case CLASS_NOTIFY_LIST_DETAIL:
			getReceiveService().doClassNotifyListDetail(request,response,je);
			break;
		case DELETE_CLASS_NOTIFY:
			getReceiveService().doDeleteClassNotify(request,response,je);
			break;
		case QUERY_SCHOOL_LIST:
			getReceiveService().doQuerySchoolList(request,response,je);
			break;
		case QUERY_DEPARTMENT_LIST:
			getReceiveService().doQueryDeparmentList(request,response,je);
			break;
		case QUERY_SCHOOL_INFO:
			getReceiveService().doQuerySchoolInfo(request,response,je);
			break;
		case SCHOOL_CALENDAR:
			getReceiveService().doQuerySchoolCalendar(request,response,je);
		    break;
		case SCHOOL_NOTIFY_FIRST:
			getReceiveService().doSchoolNotifyFirst(request,response,je);
			break;
		case CLASS_NOTIFY_FIRST:
			getReceiveService().doClassNotifyFrist(request,response,je);
			break;
		case SELECT_CONTACTS_LIST:
			getReceiveService().doQueryContactsList(request,response,je);
			break;
		default:
			responseException(response.getWriter());
		}
	}

	private int getCode(JsonElement je,HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub 
		JsonObject jo=je.getAsJsonObject();
		String codestr=jo.get("code")+"";
		if(codestr==null||"".equals(codestr)){
			responseException(response.getWriter());
		}

		return Integer.parseInt(codestr);
	}
	private void responseException(PrintWriter out){
		out.print(EXCEPTION_ERROR);
		out.flush();
		out.close();
	}
}
